package org.jeecg.modules.demo.wz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.checkerframework.checker.units.qual.C;
import org.jeecg.modules.demo.wz.entity.Servicecall;
import org.jeecg.modules.demo.wz.entity.Servicecalltype;
import org.jeecg.modules.demo.wz.mapper.ServicecallMapper;
import org.jeecg.modules.demo.wz.service.IServicecallService;
import org.jeecg.modules.demo.wz.service.IServicecalltypeService;
import org.jeecg.modules.demo.wz.utils.CityWeatherUtils;
import org.jeecg.modules.demo.wz.utils.CommonResult;
import org.jeecg.modules.demo.wz.utils.WeekUtils;
import org.jeecg.modules.demo.wz.vo.CallTypeWeekVo;
import org.jeecg.modules.demo.wz.vo.WeekCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @Description: 电话呼入情况表
 * @Author: jeecg-boot
 * @Date:   2022-03-30
 * @Version: V1.0
 */
@Service
@ConfigurationProperties(prefix = "city")
public class ServicecallServiceImpl extends ServiceImpl<ServicecallMapper, Servicecall> implements IServicecallService {

    @Autowired
    private IServicecalltypeService iServicecalltypeService;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    private String code;


    @Override
    public CommonResult countCallType() {
        List<CallTypeWeekVo> callTypeWeekVoList=new ArrayList<>();
        List<Servicecalltype> servicecalltypeList = iServicecalltypeService.list();
        int num=0;
        for (Servicecalltype servicecalltype : servicecalltypeList) {
            LambdaQueryWrapper<Servicecall> queryWrapper=new LambdaQueryWrapper<>();
            //前一周的电话呼入情况分类
            queryWrapper.between(Servicecall::getTime,
                    WeekUtils.getLastWeek(6)+ " 00:00:00",
                    WeekUtils.getLastWeek(0)+ " 23:59:59")
                    .eq(Servicecall::getCalltype,servicecalltype.getId());
            int count = count(queryWrapper);
            //封装成vo
            CallTypeWeekVo callTypeWeekVo=new CallTypeWeekVo();
            callTypeWeekVo.setId(++num);
            callTypeWeekVo.setCount(count);
            callTypeWeekVo.setCallTypeName(servicecalltype.getCalltypename());
            callTypeWeekVoList.add(callTypeWeekVo);
        }
        if (callTypeWeekVoList!=null){
            return new CommonResult(200,"查询成功",callTypeWeekVoList);
        }else {
            return new CommonResult(400,"无相关数据");
        }
    }

    @Override
    public CommonResult countCallInWeek() {
        List<WeekCountVo> weekCountVoList=new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            LambdaQueryWrapper<Servicecall> queryWrapper=new LambdaQueryWrapper<>();
            //上星期
            queryWrapper.between(Servicecall::getTime,
                    WeekUtils.getLastWeek(i)+ " 00:00:00",
                    WeekUtils.getLastWeek(i)+ " 23:59:59");
            int count = count(queryWrapper);
            //封装成vo
            WeekCountVo weekCountVo=new WeekCountVo();
            weekCountVo.setId(7-i);
            if (i==0){
                weekCountVo.setWeekDay("星期天");
            }else {
                weekCountVo.setWeekDay("星期"+(7-i));
            }
            weekCountVo.setCount(count);
            weekCountVoList.add(weekCountVo);
        }
        if (weekCountVoList!=null){
            return new CommonResult(200,"查询成功",weekCountVoList);
        }else {
            return new CommonResult(400,"无相关数据");
        }

    }

    @Override
    public CommonResult getCallCountInWeek() {
        LambdaQueryWrapper<Servicecall> queryWrapper=new LambdaQueryWrapper<>();
        //上星期
        queryWrapper.between(Servicecall::getTime,
                WeekUtils.getLastWeek(6)+ " 00:00:00",
                WeekUtils.getLastWeek(0)+ " 23:59:59");
        int count = count(queryWrapper);
        if (count>=0){
            return new CommonResult(200,"查询成功",count);
        }
        return new CommonResult(400,"数据异常");
    }

    @Override
    public CommonResult getWeather() {
        Map<String, Object> todayWeather=new HashMap<>();
        try {
            todayWeather = CityWeatherUtils.getTodayWeather(code);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (todayWeather!=null){
            return new CommonResult(200,"查询成功",todayWeather.get("weather"));
        }else {
            return new CommonResult(400,"无相关数据");
        }
    }

}
